package com.wit.health.UI;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;


import com.wit.health.MainActivity2;
import com.wit.health.R;


/**
* 登陆界面
*
* @Author liaozhaoping
* @Date 2024/5/10 11:43
*/

public class LoginActivity extends AppCompatActivity {

    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    private Button btnExit;
    private CheckBox cbRemember;

    private Button btnRegister;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        getSupportActionBar().setTitle("登陆");

        initView();

        initData();

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();


                // 跳转到 MainActivity 界面
                Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
                //将用户名传递到 MainActivity2
                intent.putExtra("USERNAME_KEY", username);
                startActivity(intent);
                finish();

                // 在这里处理登录逻辑
                // 从后端去获取用户信息，判断用户名和密码是否正确

//                NetUtil.verifyUserCredentials(LoginActivity.this, username, password, new Callback() {
//                    @Override
//                    public void onFailure(Call call, IOException e) {
//                        // Handle the error
//                        e.printStackTrace();
//                        runOnUiThread(() -> Toast.makeText(LoginActivity.this, "身份验证失败！", Toast.LENGTH_SHORT).show());
//                    }
//
//                    @Override
//                    public void onResponse(Call call, Response response) throws IOException {
//                        if (response.isSuccessful()) {
//                            // Handle the successful response
//                            String responseBody = response.body().string();
//                            runOnUiThread(() -> Toast.makeText(LoginActivity.this, "恭喜你，登陆成功！", Toast.LENGTH_SHORT).show());
//
//                            if(cbRemember.isChecked()){
//                                // 保存用户名和密码
//                                SharedPreferences sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
//                                SharedPreferences.Editor edit =sharedPreferences.edit();
//                                edit.putString("username", username);
//                                edit.putString("password", password);
//                                edit.putBoolean("isRemember", true);
//                                edit.commit();
//                            }else {
//                                // 清除保存的用户名和密码
//                                SharedPreferences sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
//                                SharedPreferences.Editor edit =sharedPreferences.edit();
//                                edit.putString("username", "");
//                                edit.putString("password", "");
//                                edit.putBoolean("isRemember", false);
//                                edit.commit();
//                            }
//                            // 跳转到 MainActivity 界面
//                            Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
//                            //将用户名传递到 MainActivity2
//                            intent.putExtra("USERNAME_KEY", username);
//                            startActivity(intent);
//                            finish();
//                        } else {
//                            // Handle the unsuccessful response
//                            runOnUiThread(() -> Toast.makeText(LoginActivity.this, "用户名或密码错误！", Toast.LENGTH_SHORT).show());
//                        }
//                    }
//                });

            }

        });

        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); // 退出当前Activity
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到 RegisterActivity 界面
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

    }

    private void initData() {
        // 读取保存的用户名和密码
        SharedPreferences sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "");
        String password = sharedPreferences.getString("password", "");
        boolean isRemember = sharedPreferences.getBoolean("isRemember", false);
        etUsername.setText(username);
        etPassword.setText(password);
        cbRemember.setChecked(isRemember);
    }

    private void initView() {

        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btn_login);
        btnExit = findViewById(R.id.btn_exit);
        cbRemember = findViewById(R.id.cb_remember_password);
        btnRegister = findViewById(R.id.btn_register);
    }
}